{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bd28bf33-9d1a-40bf-920d-944fdda30874",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import time\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import datetime\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.metrics import f1_score\n",
    "#特征抽取\n",
    "def read_feat(path, test_mode=False):\n",
    "    df = pd.read_csv(path)\n",
    "    df = df.iloc[::-1]\n",
    "\n",
    "    if test_mode:\n",
    "        df['type'] = df['type'].map({'拖网': 0, '围网': 1, '刺网': 2})\n",
    "        label = np.array(df['type'].iloc[0])\n",
    "        df = df.drop(['type'], axis=1)\n",
    "    else:\n",
    "        label = None\n",
    "\n",
    "    df['time'] = df['time'].apply(lambda x: datetime.datetime.strptime(x, \"%m%d %H:%M:%S\"))\n",
    "    df_diff = df.diff(1).iloc[1:]\n",
    "    df_diff['time_seconds'] = df_diff['time'].dt.total_seconds()\n",
    "    df['dis'] = np.sqrt(df['x'] ** 2 + df['y'] ** 2)\n",
    "    for column in list(df.columns[df.isnull().sum() > 0]): #去除含有空值的行\n",
    "        mean_val = df[column].mean()\n",
    "        df[column].fillna(mean_val, inplace=True)\n",
    "    features = np.array([df['x'].std(), df['x'].mean(), df['x'].max(), df['x'].min(),\n",
    "                df['y'].std(), df['y'].mean(), df['y'].max(), df['y'].min(),\n",
    "                df['速度'].mean(), df['速度'].std(), df['速度'].max(), df['速度'].min(),\n",
    "                df['方向'].mean(), df['方向'].std(), df['方向'].max(), df['方向'].min(),\n",
    "                ])\n",
    "    return (features, label) if len(features[np.isnan(features)]) == 0 else (None, None)\n",
    "\n",
    "#数据载入\n",
    "def load_data(X_file=\"./npy/data_x2.npy\", Y_file=\"./npy/data_y2.npy\", new=False):\n",
    "    if os.path.exists(X_file) and os.path.exists(Y_file) and not new:\n",
    "        X = np.load(X_file)\n",
    "        Y = np.load(Y_file)\n",
    "        return np.array(X), np.array(Y)\n",
    "    else:\n",
    "        path = './hy_round1_train_20200102'\n",
    "        train_file = os.listdir(path)\n",
    "        X = []\n",
    "        Y = []\n",
    "        for i, each in enumerate(train_file):\n",
    "            if not i % 1000:\n",
    "                print(i)\n",
    "            each_path = os.path.join(path, each)\n",
    "            x, y = read_feat(each_path, True)\n",
    "            if x is not None:\n",
    "                X.append(x)\n",
    "                Y.append(y)\n",
    "        X = np.array(X)\n",
    "        Y = np.array(Y)\n",
    "        np.save(X_file, X)\n",
    "        np.save(Y_file, Y)\n",
    "        return X, Y\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8cbd1a1b-94f3-4573-8aad-e74de53f83dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2\t模型训练\t训练集切分，参数配置，模型训练、调优\n",
    "# 按特定比例进行训练集切分\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.svm import SVC\n",
    "X, Y = load_data(new=False)\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=0)\n",
    "# 将参数写成字典下形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "85cf7159",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C_list: [0.001, 0.01, 0.1, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000]\n",
      "gamma_list: [1e-10, 1e-09, 1e-08, 1e-07, 1e-06, 1e-05, 0.0001, 0.001, 0.01, 0.1, 1, 10, 100, 1000]\n",
      "Fitting finished in 56 min 11 s\n",
      "Best set score:0.81\n",
      "Best parameters:{'C': 10000, 'class_weight': 'balanced', 'gamma': 1e-10, 'kernel': 'rbf'}\n",
      "Test set score:0.81\n",
      "accuracy 0.8542857142857143\n",
      "f1_score 0.8096280397995942\n"
     ]
    }
   ],
   "source": [
    "# 使用径向基核函数，用时56分钟\n",
    "C_list = [1 * 10**i for i in range(-3, 11)]\n",
    "gamma_list = [1 * 10**i for i in range(-10, 4)]\n",
    "print('C_list:', C_list)\n",
    "print('gamma_list:', gamma_list)\n",
    "parameters = [\n",
    "    {\n",
    "        'kernel': ['rbf'],   # 径向基核函数\n",
    "        'C': C_list,\n",
    "        'gamma': gamma_list,\n",
    "        'class_weight': ['balanced'] # 样本均衡度\n",
    "    },\n",
    "]\n",
    "# 参数调优\n",
    "clf = GridSearchCV(estimator=SVC(), param_grid=parameters, cv=8, n_jobs=5, scoring='f1_macro')\n",
    "start = time.time()\n",
    "clf.fit(X_train, y_train)\n",
    "elapsed = time.time() - start\n",
    "print(\"Fitting finished in %d min %d s\" % (elapsed / 60, elapsed % 60))\n",
    "print(\"Best set score:{:.2f}\".format(clf.best_score_))\n",
    "print(\"Best parameters:{}\".format(clf.best_params_))\n",
    "print(\"Test set score:{:.2f}\".format(clf.score(X_test, y_test)))\n",
    "\n",
    "# 测试\n",
    "predicted = clf.predict(X_test)  # 模型预测\n",
    "accuracy = accuracy_score(y_test, predicted)\n",
    "print(\"accuracy\", accuracy)\n",
    "print(\"f1_score\",f1_score(y_test,predicted,labels=[0,1,2],average='macro'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a2a338c7",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C_list: [0.001, 0.01, 0.1, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\52860\\anaconda3\\lib\\site-packages\\sklearn\\svm\\_base.py:301: ConvergenceWarning: Solver terminated early (max_iter=1000000).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting finished in 85 min 27 s\n",
      "Best set score:0.66\n",
      "Best parameters:{'C': 10000, 'class_weight': 'balanced', 'degree': 3, 'kernel': 'poly', 'max_iter': 1000000}\n",
      "Test set score:0.64\n",
      "accuracy 0.6828571428571428\n",
      "f1_score 0.6430178329641429\n"
     ]
    }
   ],
   "source": [
    "# 使用多项式核函数，用时85分钟\n",
    "C_list = [1 * 10**i for i in range(-3, 11)]\n",
    "print('C_list:', C_list)\n",
    "parameters = [\n",
    "    {\n",
    "        'kernel': ['poly'],   # 多项式核函数\n",
    "        'C': C_list,\n",
    "        'degree': range(2, 11),\n",
    "        'class_weight': ['balanced'], # 样本均衡度\n",
    "        'max_iter': [1000000]\n",
    "    }\n",
    "]\n",
    "# 参数调优\n",
    "clf = GridSearchCV(estimator=SVC(), param_grid=parameters, cv=8, n_jobs=5, scoring='f1_macro')\n",
    "start = time.time()\n",
    "clf.fit(X_train, y_train)\n",
    "elapsed = time.time() - start\n",
    "print(\"Fitting finished in %d min %d s\" % (elapsed / 60, elapsed % 60))\n",
    "print(\"Best set score:{:.2f}\".format(clf.best_score_))\n",
    "print(\"Best parameters:{}\".format(clf.best_params_))\n",
    "print(\"Test set score:{:.2f}\".format(clf.score(X_test, y_test)))\n",
    "\n",
    "# 测试\n",
    "predicted = clf.predict(X_test)  # 模型预测\n",
    "accuracy = accuracy_score(y_test, predicted)\n",
    "print(\"accuracy\", accuracy)\n",
    "print(\"f1_score\",f1_score(y_test,predicted,labels=[0,1,2],average='macro'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "102cc4c4-6a3e-4328-a921-f2d1ae4f7bb9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C_list: [0.001, 0.01, 0.1, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000]\n",
      "Fitting finished in 5 min 56 s\n",
      "Best set score:0.64\n",
      "Best parameters:{'C': 1000, 'class_weight': 'balanced', 'kernel': 'linear', 'max_iter': 1000000}\n",
      "Test set score:0.63\n",
      "accuracy 0.6792857142857143\n",
      "f1_score 0.6317640429893184\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\52860\\anaconda3\\lib\\site-packages\\sklearn\\svm\\_base.py:301: ConvergenceWarning: Solver terminated early (max_iter=1000000).  Consider pre-processing your data with StandardScaler or MinMaxScaler.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "# 使用线性核函数，用时5分钟\n",
    "C_list = [1 * 10**i for i in range(-3, 11)]\n",
    "print('C_list:', C_list)\n",
    "parameters = [\n",
    "    {\n",
    "        'kernel': ['linear'],   #线性核函数\n",
    "        'C': C_list,\n",
    "        'class_weight': ['balanced'], #样本均衡度\n",
    "        'max_iter': [1000000]\n",
    "    },\n",
    "]\n",
    "# 参数调优 \n",
    "clf = GridSearchCV(estimator=SVC(), param_grid=parameters, cv=8, n_jobs=5, scoring='f1_macro')\n",
    "start = time.time()\n",
    "clf.fit(X_train, y_train)\n",
    "elapsed = time.time() - start\n",
    "print(\"Fitting finished in %d min %d s\" % (elapsed / 60, elapsed % 60))\n",
    "print(\"Best set score:{:.2f}\".format(clf.best_score_))\n",
    "print(\"Best parameters:{}\".format(clf.best_params_))\n",
    "print(\"Test set score:{:.2f}\".format(clf.score(X_test, y_test)))\n",
    "\n",
    "# 测试\n",
    "predicted = clf.predict(X_test)  # 模型预测\n",
    "accuracy = accuracy_score(y_test, predicted)\n",
    "print(\"accuracy\", accuracy)\n",
    "print(\"f1_score\",f1_score(y_test,predicted,labels=[0,1,2],average='macro'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1d77cbb8-9736-4870-a882-9ac2aad1da2f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "accuracy 0.6828571428571428\n",
      "f1_score 0.6430178329641429\n"
     ]
    }
   ],
   "source": [
    "predicted = clf.predict(X_test)  # 模型预测\n",
    "accuracy = accuracy_score(y_test, predicted)\n",
    "print(\"accuracy\", accuracy)\n",
    "print(\"f1_score\",f1_score(y_test,predicted,labels=[0,1,2],average='macro'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e52f4bc1-f8d5-478c-9ada-777ad1b9f1c3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
